package org.thinkinbunny.financial.login;

import org.thinkinbunny.financial.R;
import org.thinkinbunny.financial.activity.MyActivity;
import org.thinkinbunny.financial.billentity.UserEntiry;
import org.thinkinbunny.financial.encrypt.Encrypt;
import org.thinkinbunny.financial.http.HttpTools;
import org.thinkinbunny.financial.util.CommonUtil;
import org.thinkinbunny.financial.util.Log;
import org.thinkinbunny.financial.util.ShowTools;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends MyActivity {
	EditText username_et, password_et, password_rp_et, email_et;
	CheckBox RegisterAccept;
	Button RegisterBTN;
	Encrypt encrypt = new Encrypt();
	UserEntiry userEntiry;
	TextView registercontentTextView;
	private ProgressDialog mpDialog;
	private Dialog dialog;
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 0:
				mpDialog.show();
				break;
			case 1:
				mpDialog.dismiss();
				break;

			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_register);
		init_UI();
		super.onCreate(savedInstanceState);

	}

	private void init_UI() {
		// TODO Auto-generated method stub
		mpDialog = new ProgressDialog(RegisterActivity.this);
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
		mpDialog.setTitle(getString(R.string.registering));// 设置标题
		// mpDialog.setIcon(R.drawable.ic_launcher);// 设置图标
		mpDialog.setMessage(getString(R.string.registerMsg));
		mpDialog.setIndeterminate(false);// 设置进度条是否为不明确

		mpDialog.setCancelable(false);// 设置进度条是否可以按退回键取消
		username_et = (EditText) findViewById(R.id.username_et);
		password_et = (EditText) findViewById(R.id.password_et);
		password_rp_et = (EditText) findViewById(R.id.password_rp_et);
		email_et = (EditText) findViewById(R.id.email_et);
		RegisterAccept = (CheckBox) findViewById(R.id.RegisterAccept);
		RegisterBTN = (Button) findViewById(R.id.RegisterBTN);
		registercontentTextView=(TextView) findViewById(R.id.Register_conetnt);
		registercontentTextView.setOnClickListener(this);
		RegisterBTN.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.RegisterBTN: {

			if (checkInfo()) {

				Message msgMessage = mHandler.obtainMessage(0);
				mHandler.sendMessage(msgMessage);
				new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						DoRegister();
						super.run();
					}

				}.start();
			}

		}

		break;
		case R.id.Register_conetnt:
		{showNotice();}
		break;


		default:
			break;
		}
		super.onClick(v);
	}
	private void showNotice (){
		LayoutInflater factory = LayoutInflater.from(this);
		final View buttonView = factory.inflate(R.layout.dialog_pronouncement,
				null);
		dialog = new Dialog(this){
			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					//					showExitDialog();
					return true;
				}
				return super.onKeyDown(keyCode, event);
			}
		};
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setCancelable(false);
		//		dialog.setTitle("");
		dialog.setContentView(buttonView);
		Button agree_button = (Button) buttonView.findViewById(R.id.agree_button);
		agree_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//	MiscToolBox.saveShowNotice(false);
				dialog.dismiss();
				//init();
			}
		});
		Button refuse_button = (Button) buttonView.findViewById(R.id.refuse_button);
		refuse_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(dialog!=null){
					dialog.dismiss();
					finish();
				}
			}
		});
		//		View launchView = (View)Launch.this.findViewById(R.id.Splash);
		//		launchView.setBackgroundColor(0xFF000000);
		dialog.show();
	}
	private void DoRegister() {
		// TODO Auto-generated method stub

		userEntiry = new UserEntiry();
		userEntiry.setUsername(username_et.getText().toString().trim());
		userEntiry.setPassword(EncryptPasswordWithSHA1(password_rp_et.getText()
				.toString().trim()));
		userEntiry.setEmail(email_et.getText().toString().trim());
		// userEntiry.setUsername(login_input_name.getText().toString().trim());
		// if (pref.getBoolean("saved", false)) {
		// userEntiry.setPassword(login_input_password.getText().toString()
		// .trim());
		// } else {
		// userEntiry.setPassword(EncryptPasswordWithSHA1(login_input_password
		// .getText().toString().trim()));
		// }
		//
		// userEntiry.setRemember(remember_user.isChecked());

		UserEntiry temp = HttpTools
				.HttpGetDataCheckUser("register", userEntiry);
		if (temp == null) {
			ShowTools.toastInThread(getString(R.string.registerErr));
			Log.i("RegisterActivity", "register error");
		} else {
			// if (!readUser(false)) {

			// saveUser(userEntiry);
			Intent intent = new Intent(RegisterActivity.this,
					LoginActivity.class);
			// startActivity();
			Bundle bundle = new Bundle();
			userEntiry.setPassword(password_rp_et.getText().toString().trim());
			bundle.putSerializable("userEntiry", userEntiry);
			intent.putExtra("userEntiryBundle", bundle);
			startActivity(intent);
			finish();
			// }
		}
		Message msgMessage = mHandler.obtainMessage(1);
		mHandler.sendMessage(msgMessage);

	}

	private boolean checkInfo() {
		if (!RegisterAccept.isChecked()) {
			ShowTools.toastInThread(getString(R.string.registerAccept));
			return false;
		}
		if (!CommonUtil.checkUsername(username_et.getText().toString().trim(), 6, 16)) {
			username_et.setError(getString(R.string.registerSAccount));
			return false;
		}
		if (!CommonUtil.checkPassword(password_et.getText().toString().trim(), 6, 16)) {
			password_et.setError(getString(R.string.registerPWD));
			return false;
		}

		if (!password_et.getText().toString().trim()
				.equals(password_rp_et.getText().toString().trim())) {
			password_rp_et.setError(getString(R.string.registerpwdRepeat));
			return false;
		}
		if (!CommonUtil.checkEmailWithSuffix(email_et.getText().toString().trim())) {
			email_et.setError(getString(R.string.registerErrmail));
			return false;
		}

		return true;

	}

	private String EncryptPasswordWithSHA1(String str) {

		return encrypt.getSHA(str);
	}
}
